'''
假设要在 Minecraft 里面实现一种连锁挖矿的功能，随机生成一个矩阵，
0，1，2 代表不同的方块类型。请完善 Destroy 方法，将破坏的方块改为
#，并输出修改后的矩阵。
*连锁挖矿：破坏一个方块之后，接连破坏所有邻接的相同方块。
'''
import random


def print2D(lists):
    for row in lists:
        print(row)
    print()


def Destroy(x, y, lists):
    assert x >= 0 and x < len(lists)
    assert y >= 0 and y < len(lists[0])
    m = len(lists)
    n = len(lists[0])
    
    def DestroyHelper(i, j, block):
        if not lists[i][j] == block: return
        lists[i][j] = '#'
        if i-1 >= 0 and lists[i-1][j] == block: DestroyHelper(i-1, j, block)
        if i+1 <  m and lists[i+1][j] == block: DestroyHelper(i+1, j, block)
        if j-1 >= 0 and lists[i][j-1] == block: DestroyHelper(i, j-1, block)
        if j+1 <  n and lists[i][j+1] == block: DestroyHelper(i, j+1, block)
    
    DestroyHelper(x, y, lists[x][y])
    print2D(lists)


# Initialize
size = 8
blocks = [[0 for _ in range(size)] for _ in range(size)]
for x in range(size):
    for y in range(size):
        blocks[x][y] = random.choice([0, 1, 2])

# Destroy blocks
x0, y0 = 5, 5
print2D(blocks)
print('block type: {}\n'.format(blocks[x0][y0]))
Destroy(x0, y0, blocks)
